let App = getApp()
let uploadImage = require('../../utils/uploadFile.js');
Page({
  data: {
    stars: [0, 1, 2, 3, 4],
    normalSrc: '../../assets/images/star-default.png',
    selectedSrc: '../../assets/images/star-active.png',
    key: 0, // 评分
    imgList: {
      tempFilePaths: [],
      imgList: []
    }
  },
  onLoad (option) {
    this.setData({orderId: option.id})
  },
  select (e) { // 点击整颗星
    let key = e.currentTarget.dataset.key
    this.setData({ key: key }) 
  },
  bindTextAreaBlur (e) { // 监听到新输入的值
    this.setData({ desc: e.detail.value })
  },
  joinPicture (e) { // 添加图片
    let imgList = this.data.imgList
    let that = this;
    let tempFilePaths = imgList.tempFilePaths
    if (tempFilePaths.length >= 3) {
      wx.showModal({
        title: '',
        content: '最多上传三张图片',
        showCancel: false,
      })
      return;
    }
    wx.showActionSheet({
      itemList: ["从相册中选择", "拍照"],
      itemColor: "#333",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage("album", tempFilePaths)
          } else if (res.tapIndex == 1) {
            that.chooseWxImage("camera", tempFilePaths)
          }
        }
      }
    })
  },
  chooseWxImage (type, list) {
    let img = list
    let len = img.length
    let that = this
    wx.chooseImage({
      count: 9,
      sizeType: ["original", "compressed"],
      sourceType: [type],
      success: function (res) {
        let addImg = res.tempFilePaths
        let addLen = addImg.length
        let addAray = []
        if ((len + addLen) > 3) {
          for (let i = 0; i < (addLen - len); i++) {
            addAray.push(addImg[i])
          }
        } else {
          for (let j = 0; j < addLen; j++) {
            addAray.push(addImg[j])
          }
        }
        that.upload(addAray)
      },
    })
  },
  upload (img) { 
    // 图片上传oss
    let that = this
    let { imgList } = that.data
    for (let i = 0; i < img.length; i++) {
      wx.showLoading({
        title: '上传中' + (i + 1) + '/' + img.length,
        mask: true
      })
      uploadImage(img[i], 'weapp/', function (result) {
         // weapp/ 上传文件夹
        imgList.tempFilePaths.push(result)
        that.setData({imgList})
        wx.hideLoading()
      }, function (result) {
        wx.hideLoading()
      })
    }
  },
  clearImg (e) {//删除图片
    let index = e.currentTarget.dataset.index
    let imgList = this.data.imgList
    let img = imgList.tempFilePaths
    img.splice(index, 1)
    this.setData({
      imgList: imgList
    })
    // this.upLoadImg(img)
  },
  submit (e) {//提交发布
    let { imgList, desc, key, orderId } = this.data
    let params = {
      orderId: orderId,
      sorce: key,
      content: desc,
      imgs: imgList.tempFilePaths,
      fromId: e.detail.formId
    }
    // wx.showLoading({
    //   title: '提交中',
    //   mask: true
    // })
    App.HttpService.userRevies(params).then(res => {
      // wx.hideLoading()

   
      wx.showToast({
          title: '当前增加 0 积分',
        icon: 'none',
        duration: 1500
      })
      setTimeout(function () {

        wx.navigateBack()
      }, 2000)


      var pages = getCurrentPages()
      var prePage = pages[pages.length - 2]
      prePage.onPullDownRefresh()
 

      if(res.data.code == 200){
        // let data = res.data.data;
        // let score = data.score;
        // let orderSn = data.orderSn;
        // let revieDate = data.revieDate;
        // let token = data.token;
        // let userScore = data.userScore;
        // let template_id = "lCH9G5V8CBR4gLnb-3kCOqkm4NxdRQrdPTWFpV1LV8E";
        // let toUser = wx.getStorageSync('openid');
        // let prepay_id = e.detail.formId;
        // let postData = {
        //   "touser": toUser,
        //   "template_id": template_id,
        //   "form_id": prepay_id,
        //   "data": {
        //     "keyword1": {
        //       "value": orderSn
        //     },
        //     "keyword2": {
        //       "value": score + "颗星"
        //     },
        //     "keyword3": {
        //       "value": revieDate
        //     },
        //     "keyword4": {
        //       "value": "您此次获得的积分为" + userScore
        //     }
        //   }
        // }

        // 发送模板消息
        // wx.request({
        //   url: `https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=${token}`,
        //   method: 'POST',
        //   data: postData,
        //   dataType: 'json',
        //   success(res) {
        //     console.log("消息")
        //     console.log(res);
        //   }
        // })
      }
    }).catch(e => {
      wx.hideLoading()
    })
  }
})